package com.example.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.core.constants.Constants;
import com.example.common.core.enums.ResultCode;
import com.example.common.core.utils.ThreadLocalUtil;
import com.example.common.security.exception.ServiceException;
import com.example.user.domain.chat.GroupMember;
import com.example.user.domain.classroom.Classroom;
import com.example.user.domain.classroom.dto.BuyClassroomSaveDTO;
import com.example.user.domain.classroom.dto.ClassroomQueryDTO;
import com.example.user.domain.classroom.vo.ClassroomDetailVO;
import com.example.user.domain.classroom.vo.ClassroomVO;
import com.example.user.domain.student.Student;
import com.example.user.mapper.StudentMapper;
import com.example.user.mapper.classroom.ClassroomMapper;
import com.example.user.service.IClassroomService;
import com.example.user.service.IGroupService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassroomService implements IClassroomService {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private ClassroomMapper classroomMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<ClassroomVO> list(ClassroomQueryDTO classroomQueryDTO) {
        Long teacherId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        classroomQueryDTO.setTeacherId(teacherId);
        PageHelper.startPage(classroomQueryDTO.getPageNum(), classroomQueryDTO.getPageSize());

        return classroomMapper.selectClassroomList(classroomQueryDTO);
    }

    @Override
    public List<ClassroomVO> availableClass(ClassroomQueryDTO classroomQueryDTO) {
        PageHelper.startPage(classroomQueryDTO.getPageNum(), classroomQueryDTO.getPageSize());
        return classroomMapper.availableClass(classroomQueryDTO);
    }

    @Override
    public ClassroomDetailVO classroomDetails(Long classroomId) {
        Classroom classroom = classroomMapper.selectById(classroomId);
        if (classroom == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        ClassroomDetailVO classroomDetailVO = new ClassroomDetailVO();
        BeanUtil.copyProperties(classroom, classroomDetailVO);
        return classroomDetailVO;
    }

    @Override
    public Boolean enrolledInAClass(Long classroomId) {
        Long studentId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        Classroom classroom = classroomMapper.selectById(classroomId);
        if (classroom == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        Student student = studentMapper.selectById(studentId);
        if (student!=null&&student.getClassroomId()!=null&&student.getClassroomId().equals(classroomId)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int buyClassroom(BuyClassroomSaveDTO buyClassroomSaveDTO) {
        // 参数验证
        if (buyClassroomSaveDTO == null || buyClassroomSaveDTO.getClassroomId() == null) {
            throw new ServiceException(ResultCode.FAILED);
        }

        Long studentId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        Long classroomId = buyClassroomSaveDTO.getClassroomId();

        // 验证班级是否存在
        Classroom classroom = classroomMapper.selectById(classroomId);
        if (classroom == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }

        // 验证学生是否存在
        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        // 检查学生是否已经加入其他班级
        if (student.getClassroomId() != null) {
            throw new ServiceException(ResultCode.FAILED_DOUBLE_ENROLLMENT);
        }

        // 更新学生班级信息
        student.setClassroomId(classroomId);

        // 加入群聊

        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(classroom.getClassroomId());
        groupMember.setUserId(studentId);
        groupMember.setNickname(ThreadLocalUtil.get(Constants.USER_NAME,String.class));
        groupMember.setIdentity(ThreadLocalUtil.get(Constants.USER_IDENTITY,Integer.class));
        groupService.addGroupMember(groupMember);
        return studentMapper.updateById(student);
    }

    @Override
    public ClassroomVO myClassroom() {
        Long studentId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        Student student = studentMapper.selectById(studentId);
        if (student.getClassroomId()==null) {
            return null;
        }
        Classroom classroom = classroomMapper.selectById(student.getClassroomId());
        if (classroom == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        ClassroomVO classroomVO = new ClassroomVO();
        BeanUtil.copyProperties(classroom, classroomVO);
        return classroomVO;
    }
}